﻿using ClientServer.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace updServervice
{
    class Program
    {
        private static IPAddress GroupAddress = IPAddress.Parse("224.100.0.1"); //定义组播地址
        private static int GroupPort = 11000;


        private float increaseValue = 0.1f;
        static void Main(string[] args)
        {
            UdpClient myUdpClient = new UdpClient(); //建立发送端的UdpClient实例
            IPEndPoint groupEP = new IPEndPoint(GroupAddress, GroupPort);//定义或设置IP地址与端口
            Console.WriteLine("组播发送端已经启动");
            while (true)
            {
                
                //每秒发送16次
                for(int i=0;i<16;i++)
                {
                    byte[] bytes = GetData();
                    myUdpClient.Send(bytes, bytes.Length, groupEP);

                    Thread.Sleep(63);
                }
                crease_value = crease_value + 0.1f;
               
            }
           
        }


        private static float crease_value = 1;
        private static byte[] GetData()
        {
            List<byte> result = new List<byte>();

            result.Add(0x0A);
            result.Add(0x0D);
            result.Add(0x0D);
            result.Add(0x0D);

            //小时
            float hour = DateTime.UtcNow.Hour;
            byte[] hour_byte = BitConverter.GetBytes(hour);
            result.AddRange(hour_byte);


            //分钟
            float minute = DateTime.UtcNow.Minute;
            byte[] minute_byte = BitConverter.GetBytes(minute);
            result.AddRange(minute_byte);

            //秒
            float second = DateTime.UtcNow.Second;
            byte[] second_byte = BitConverter.GetBytes(second);
            result.AddRange(second_byte);


            //毫秒
            float millisecond = (float)RandomUtils.NextInt(1, 60);
            byte[] millisecond_byte = BitConverter.GetBytes(millisecond);
            result.AddRange(millisecond_byte);
            //260个值
            for(int i=0;i<260;i++)
            {
                float value = (float)RandomUtils.NextDouble(crease_value, crease_value + 2.0f, 3);
                byte[] value_byte = BitConverter.GetBytes(value);
                result.AddRange(value_byte);
            }

           
            return result.ToArray();
        }
    }
}
